import http from '../utils/request';
import { encrypt } from '../utils/common';

// Auth Center API前缀
const AUTH_CENTER_PREFIX = '[AuthCenter]';

// 类型定义
interface LoginData {
  username: string;
  password: string;
}

interface RegisterData {
  username: string;
  password: string;
  email?: string;
}

interface RegisterResponse {
  code: number;
  message: string;
  data?: {
    userId: number;
    username: string;
  };
}

interface LoginResponse {
  code: number;
  message: string;
  data?: {
    accessToken: string;
    refreshToken: string;
    userId: number;
    username: string;
    roles: string[];
  };
}

interface RefreshTokenResponse {
  code: number;
  message: string;
  data?: {
    accessToken: string;
    refreshToken: string;
  };
}

interface ProfileResponse {
  code: number;
  message: string;
  data?: {
    id: number;
    username: string;
    email?: string;
    roles?: { code: string; name: string }[];
  };
}

/**
 * Auth Center API
 */
export const authCenterApi = {
  /**
   * 用户登录
   * @param {LoginData} data - 登录凭据
   * @returns {Promise<LoginResponse>} - HTTP POST请求的Promise对象
   */
  login: (data: LoginData): Promise<LoginResponse> => {
    return http.post('/auth-center/login', data, {
      showLoading: true,
      showError: true
    });
  },
  
  /**
   * 刷新令牌
   * @param {string} refreshToken - 刷新令牌
   * @returns {Promise<RefreshTokenResponse>} - HTTP POST请求的Promise对象
   */
  refreshToken: (refreshToken: string): Promise<RefreshTokenResponse> => {
    return http.post('/auth-center/refresh-token', { refreshToken }, {
      showError: true
    });
  },
  
  /**
   * 登出系统
   * @returns {Promise<any>} - HTTP POST请求的Promise对象
   */
  logout: (): Promise<any> => {
    return http.post('/auth-center/logout', {}, {
      showLoading: true,
      showSuccess: true,
      successMsg: '已安全退出系统'
    });
  },
  
  /**
   * 获取用户资料
   * @returns {Promise<ProfileResponse>} - HTTP GET请求的Promise对象
   */
  getProfile: (): Promise<ProfileResponse> => {
    return http.get('/user/my-profile');
  },
  
  /**
   * 用户注册
   * @param {RegisterData} data - 注册信息
   * @returns {Promise<RegisterResponse>} - HTTP POST请求的Promise对象
   */
  register: (data: RegisterData): Promise<RegisterResponse> => {
    return http.post('/user/register', data, {
      showLoading: true,
      showError: true
    });
  }
}; 